home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_02 / overload.cpp < prev    next >
C/C++ Source or Header  |  1992-01-18  |  1KB  |  49 lines

  1.                                      // Chapter 4 - Program 6
  2. #include <iostream.h>
  3.  
  4. overload do_stuff;             // This is optional
  5.  
  6. int do_stuff(const int);       // This squares an integer
  7. int do_stuff(float);           // This triples a float & returns int
  8. float do_stuff(const float, float); // This averages two floats
  9.  
  10. main()
  11. {
  12. int index = 12;
  13. float length = 14.33;
  14. float height = 34.33;
  15.  
  16.    cout << "12 squared is "    << do_stuff(index)         << "\n";
  17.    cout << "24 squared is "    << do_stuff(2 * index)     << "\n";
  18.    cout << "Three lengths is " << do_stuff(length)        << "\n";
  19.    cout << "Three heights is " << do_stuff(height)        << "\n";
  20.    cout << "The average is "   << do_stuff(length,height) << "\n";
  21. }
  22.  
  23. int do_stuff(const int in_value)      // This squares an integer
  24. {
  25.    return in_value * in_value;
  26. }
  27.  
  28. int do_stuff(float in_value)    // Triples a float & return int
  29. {
  30.    return (int)(3.0 * in_value);
  31. }
  32.  
  33.                                       // This averages two floats
  34. float do_stuff(const float in1, float in2)
  35. {
  36.    return (in1 + in2)/2.0;
  37. }
  38.  
  39.  
  40.  
  41.  
  42. // Result of execution
  43. //
  44. // 12 squared is 144
  45. // 24 squared is 576
  46. // Three lengths is 42
  47. // Three heights is 102
  48. // The average is 24.330002
  49.